<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6 0006
 * Time: 10:38
 */
//static 静态

//Const 大写的常量名 = 值；
//1、::叫做作用域操作符
//
//2、常量在声明的时候必须赋值！
//
//3、声明常量的过程中，不需要其他的修饰符


//功能:数据库操作类
class MySql{
    const HOST  = "localhost";
    const DBNAME = "user";
    const USER   = "root";
    const PWD    = "root";

    public $code = "utf8";
    public $link = null;
    public $data = array();
    public $result = null;
    //数据库链接方法
    public function _connect(){
        if(self::HOST != "" && self::USER != ""){
            $this -> link = mysql_connect(self::HOST,self::USER,self::PWD);
            if(is_resource($this -> link)){
                $db = mysql_select_db(self::DBNAME,$this -> link);
                if($db){
                    $this -> setCode();
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    // 设置编码
    public function setCode($code = ""){
        if($code != "")
            $this -> code = $code;
        mysql_query("set names ".$this -> code);
    }
}

$db = new MySql;
if($db -> _connect()) echo "connect ok";
else echo "connect error";
echo mysql_error();
?>